﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mbs.Utils.AI
{
    /// <summary>
    /// AI服务接口定义
    /// </summary>
    public interface IAiService
    {
        /// <summary>
        /// 设置服务的基础URL
        /// </summary>
        /// <param name="baseUrl">基础URL</param>
        void SetBaseUrl(string baseUrl);
        /// <summary>
        /// 获取可用的模型列表
        /// </summary>
        /// <returns>包含执行结果和模型列表的响应</returns>
        Task<AiResponse<List<string>>> GetAvailableModelsAsync();
        /// <summary>
        /// 与AI进行对话
        /// </summary>
        /// <param name="prompt">用户输入</param>
        /// <param name="model">使用的模型</param>
        /// <param name="maxTokens">最大token数</param>
        /// <param name="temperature">温度参数</param>
        /// <returns>包含执行结果和AI响应的响应</returns>
        Task<AiResponse<string>> ChatAsync(string prompt, string model = null, int? maxTokens = null, float? temperature = null);
        /// <summary>
        /// 生成嵌入向量
        /// </summary>
        /// <param name="input">输入文本</param>
        /// <param name="model">使用的模型</param>
        /// <returns>包含执行结果和嵌入向量的响应</returns>
        Task<AiResponse<float[]>> GenerateEmbeddingsAsync(string input, string model = null);
        /// <summary>
        /// 生成图片
        /// </summary>
        /// <param name="prompt">图片描述</param>
        /// <param name="model">使用的模型</param>
        /// <param name="size">图片尺寸</param>
        /// <param name="quality">图片质量</param>
        /// <returns>包含执行结果和图片URL/base64编码的响应</returns>
        Task<AiResponse<string>> GenerateImageAsync(string prompt, string model = null, string size = null, string quality = null);
        /// <summary>
        /// 语音转文字
        /// </summary>
        /// <param name="audioData">音频数据</param>
        /// <param name="model">使用的模型</param>
        /// <returns>包含执行结果和转换后文本的响应</returns>
        Task<AiResponse<string>> TranscribeAudioAsync(byte[] audioData, string model = null);

        /// <summary>
        /// 带历史记录的AI对话
        /// </summary>
        /// <param name="messages">历史消息列表</param>
        /// <param name="model">使用的模型</param>
        /// <param name="maxTokens">最大token数</param>
        /// <param name="temperature">温度参数</param>
        /// <returns>包含执行结果和AI响应的响应</returns>
        Task<AiResponse<string>> ChatWithHistoryAsync(List<ChatMessage> messages, string model = null, int? maxTokens = null, float? temperature = null);
    }
    /// <summary>
    /// AI服务响应通用结构
    /// </summary>
    /// <typeparam name="T">返回数据类型</typeparam>
    public class AiResponse<T>
    {
        /// <summary>
        /// 是否执行成功
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 执行耗时(毫秒)
        /// </summary>
        public long ElapsedMilliseconds { get; set; }
        /// <summary>
        /// 错误信息(失败时)
        /// </summary>
        public string ErrorMessage { get; set; }
        /// <summary>
        /// 错误异常(失败时)
        /// </summary>
        public Exception Exception { get; set; }
        /// <summary>
        /// 返回数据(成功时)
        /// </summary>
        public T Data { get; set; }
        /// <summary>
        /// 原始响应内容(调试用)
        /// </summary>
        public string RawResponse { get; set; }
        /// <summary>
        /// 创建成功的响应
        /// </summary>
        public static AiResponse<T> Ok(T data, long elapsed, string rawResponse = null)
        {
            return new AiResponse<T>
            {
                Success = true,
                Data = data,
                ElapsedMilliseconds = elapsed,
                RawResponse = rawResponse
            };
        }
        /// <summary>
        /// 创建失败的响应
        /// </summary>
        public static AiResponse<T> Fail(string error, Exception ex = null, string rawResponse = null)
        {
            return new AiResponse<T>
            {
                Success = false,
                ErrorMessage = error,
                Exception = ex,
                RawResponse = rawResponse
            };
        }

    }

    /// <summary>
    /// 聊天消息结构
    /// </summary>
    public class ChatMessage
    {
        /// <summary>
        /// 消息角色 (system/user/assistant)
        /// </summary>
        public string Role { get; set; }

        /// <summary>
        /// 消息内容
        /// </summary>
        public string Content { get; set; }
    }
}
